package com.atguigu.mybatis.test;

import com.atguigu.mybatis.bean.EmpStatus;
import com.atguigu.mybatis.bean.Employee2;
import com.atguigu.mybatis.bean.OraclePage;
import com.atguigu.mybatis.dao.EmployeeMapper2;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;

/**
 * 分页、枚举、批量插入、存储过程
 */
public class MyBatisTest5_Page {

    private SqlSessionFactory getSqlSessionFactory() throws IOException {
        String resource = "mybatis-config3.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        return new SqlSessionFactoryBuilder().build(inputStream);
    }


    @Test
    public void testEnumUse() {
        EmpStatus login = EmpStatus.LOGIN;
        System.out.println("枚举的索引：" + login.ordinal());
        System.out.println("枚举的名字：" + login.name());

        System.out.println("枚举的状态码：" + login.getCode());
        System.out.println("枚举的提示消息：" + login.getMsg());
    }

    /**
     * 枚举
     */
    @Test
    public void test() throws IOException {
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
        SqlSession openSession = sqlSessionFactory.openSession();
        try {
            Employee2 employee = openSession.selectOne(
                    "com.atguigu.mybatis.dao.EmployeeMapper2.getEmpById", 1);
            System.out.println(employee);
            System.out.println(employee.getEmpStatus());
        } finally {
            openSession.close();
        }
    }

    /**
     * 默认mybatis在处理枚举对象的时候保存的是枚举的名字：EnumTypeHandler
     * 改变使用：EnumOrdinalTypeHandler
     */
    @Test
    public void testEnum() throws IOException {
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
        SqlSession openSession = sqlSessionFactory.openSession();
        try {
            EmployeeMapper2 mapper = openSession.getMapper(EmployeeMapper2.class);
//			Employee2 employee = new Employee2("test_enum", "enum@atguigu.com","1");
//			mapper.addEmp(employee);
//			System.out.println("保存成功"+employee.getId());
//			openSession.commit();
            Employee2 emp = mapper.getEmpById(1002);
            System.out.println(emp.getEmpStatus());
        } finally {
            openSession.close();
        }
    }

    /**
     * 使用分页插件，分页查询
     */
    @Test
    public void test01() throws IOException {
        // 1、获取sqlSessionFactory对象
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
        // 2、获取sqlSession对象
        SqlSession openSession = sqlSessionFactory.openSession();
        try {
            EmployeeMapper2 mapper = openSession.getMapper(EmployeeMapper2.class);
            Page<Object> page = PageHelper.startPage(3, 4);

            List<Employee2> emps = mapper.getEmps();
            //传入要连续显示多少页
            PageInfo<Employee2> info = new PageInfo<>(emps, 5);
            for (Employee2 employee : emps) {
                System.out.println(employee);
            }
			/*System.out.println("当前页码："+page.getPageNum());
			System.out.println("总记录数："+page.getTotal());
			System.out.println("每页的记录数："+page.getPageSize());
			System.out.println("总页码："+page.getPages());*/
            ///xxx
            System.out.println("当前页码：" + info.getPageNum());
            System.out.println("总记录数：" + info.getTotal());
            System.out.println("每页的记录数：" + info.getPageSize());
            System.out.println("总页码：" + info.getPages());
            System.out.println("是否第一页：" + info.isIsFirstPage());
            System.out.println("连续显示的页码：");
            int[] nums = info.getNavigatepageNums();
            for (int i = 0; i < nums.length; i++) {
                System.out.println(nums[i]);
            }
            //xxxx
        } finally {
            openSession.close();
        }
    }

    /**
     * 批量插入
     */
    @Test
    public void testBatch() throws IOException {
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();

        //可以执行批量操作的sqlSession
        SqlSession openSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
        long start = System.currentTimeMillis();
        try {
            EmployeeMapper2 mapper = openSession.getMapper(EmployeeMapper2.class);
            for (int i = 0; i < 10000; i++) {
                mapper.addEmp(new Employee2(UUID.randomUUID().toString().substring(0, 5), "b", "1"));
            }
            openSession.commit();
            long end = System.currentTimeMillis();
            //批量：（预编译sql一次==>设置参数===>10000次===>执行（1次））
            //Parameters: 616c1(String), b(String), 1(String)==>4598
            //非批量：（预编译sql=设置参数=执行）==》10000    10200
            System.out.println("执行时长：" + (end - start));
        } finally {
            openSession.close();
        }
    }

    /**
     * oracle分页：
     * 借助rownum：行号；子查询；
     * 存储过程包装分页逻辑
     */
    @Test
    public void testProcedure() throws IOException {
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
        SqlSession openSession = sqlSessionFactory.openSession();
        try {
            EmployeeMapper2 mapper = openSession.getMapper(EmployeeMapper2.class);
            OraclePage page = new OraclePage();
            page.setStart(1);
            page.setEnd(5);
            mapper.getPageByProcedure(page);

            System.out.println("总记录数：" + page.getCount());
            System.out.println("查出的数据：" + page.getEmps().size());
            System.out.println("查出的数据：" + page.getEmps());
        } finally {
            openSession.close();
        }
    }

}
